package com.example.administrator.hqq.broadcast;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;


import com.blankj.utilcode.util.SPUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.utils.VoiceAndVIbratorUtils;

import io.rong.imlib.model.Conversation;
import io.rong.push.notification.PushMessageReceiver;
import io.rong.push.notification.PushNotificationMessage;


public class SealNotificationReceiver extends PushMessageReceiver {

    @Override
    public boolean onNotificationMessageArrived(Context context, PushNotificationMessage pushNotificationMessage) {
        if (SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).getBoolean(LoongsConfig.MessageNotification.dialogueNotification,true))
        {
            showNotification(context,pushNotificationMessage);
            try {
                VoiceAndVIbratorUtils.playVoice(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    private void showNotification(Context context, PushNotificationMessage pushNotificationMessage) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri.Builder builder = Uri.parse("rong://" + context.getPackageName()).buildUpon();
        builder.appendPath("conversation").appendPath(pushNotificationMessage
                .getConversationType().getName())
                .appendQueryParameter("targetId", pushNotificationMessage.getTargetId())
                .appendQueryParameter("title", pushNotificationMessage.getPushTitle());
        Uri uri = builder.build();
        intent.setData(uri);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Notification.Builder builder1 = new Notification.Builder(context);
        builder1.setContentTitle(pushNotificationMessage.getPushTitle());//设置标题，必要
        builder1.setContentText(pushNotificationMessage.getPushContent());//设置内容，必要
        builder1.setWhen(System.currentTimeMillis());//设置时间，默认设置，可以忽略
        builder1.setSmallIcon(R.mipmap.logo);//设置通知栏的小图标，必须设置
        builder1.setAutoCancel(true);//设置自动删除，点击通知栏信息之后系统会自动将状态栏的通知删除，要与setContentIntent连用
        builder1.setContentIntent(pendingIntent);
        builder1.setTicker("new message");// 第一次提示消失的时候显示在通知栏上的
        builder1.setOngoing(true);
        builder1.setNumber(20);
        Notification build = builder1.build();
        manger.notify(LoongsConfig.MessageNotification.dialogueNotificationId, build);//id为1
    }

    @Override
    public boolean onNotificationMessageClicked(Context context, PushNotificationMessage pushNotificationMessage) {
        Log.e("kang", "onNotificationMessageArrived: " + pushNotificationMessage.getPushTitle());
        SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put(LoongsConfig.Conversation.ConversationID, pushNotificationMessage.getTargetId());
        if (pushNotificationMessage.getConversationType().equals(Conversation.ConversationType.PRIVATE)) {
            SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put( LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.PRIVATE);
        } else {
            SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.GROUP);
        }
        return false;
    }
}
